#!/usr/local/bin/perl
################################################################
# date: 06/11/08
# last update: 06/11/08
################################################################
=head1 DESCRIPTION
Generates the SQL script to upload Blast results and parameters.
=head1 AUTHOR - Luis M. Rodriguez-R
Email  : me@miguel.weapps.com
=cut
use strict;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init($INFO);
use Bio::SearchIO;

my $file = $ARGV[0] or LOGDIE "You must define the entry file";
LOGDIE "File $file doesn't exists" unless -e $file;
my $db = $ARGV[1] or LOGDIE "You must define the database";

my $blastObj = Bio::SearchIO->new(
			-file => $file,
			-format => 'blast');
my $i=0;
my $BlastParam_idBlastParam = int(90000000+100000*rand);
my $blastParam;

print "USE `$db`;\n";
while( my $result = $blastObj->next_result() ) { # $result-> Bio::Search::Result::ResultI
	while( my $hit = $result->next_hit() ) { # $hit-> Bio::Search::Hit::HitI
		while( my $hsp = $hit->next_hsp() ) { # $hsp-> Bio::Search::HSP::HSPI
			
			# my $idBlastResult
			# my $BlastParam_idBlastParam = 0; # ToDo
			my $queryDef = $result->query_description();
			$queryDef =~ s/\'/\\\'/g;
			my $queryLen = $result->query_length();
			my $queryID = $result->query_name();
			my $hitNum = $hit->n();
			my $hitID = $hit->name();
			my $hitDefinition = $hit->description();
			$hitDefinition =~ s/\'/\\\'/g;
			my $hitAccession = $hit->accession();
			my $hitLen = $hit->length();
			my $hspNum = $hsp->n();
			my $hspBitScore = $hsp->bits();
			my $hspScore = $hsp->score();
			my $hspEvalue = $hsp->evalue();
			my $hspQueryFrom = $hsp->start('query');
			my $hspQueryTo = $hsp->end('query');
			my $hspHitFrom = $hsp->start('sbjct');
			my $hspHitTo = $hsp->end('sbjct');
			my $hspQueryFrame = $hsp->strand('query');
			my $hspHitFrame = $hsp->strand('sbjct');
			my $hspIdentity = int($hsp->frac_identical('total')*100);
			my $hspPositive = int($hsp->frac_conserved('total')*100);
			my $hspGaps = $hsp->gaps('total');
			my $hspAlignLen = $hsp->length('total');
			my $hspQuerySeq = $hsp->seq_str('query');
			my $hspHitSeq = $hsp->seq_str('sbjct');
			my $hspMidLine = $hsp->seq_str('match');
			
			print "INSERT INTO `BlastResult`(`idBlastResult`,`BlastParam_idBlastParam`,`queryDef`,`queryLen`,`queryID`,`hitNum`,`hitID`,`hitDefinition`,`hitAccession`,`hitLen`,`hspNum`,`hspBitScore`,`hspScore`,`hspEvalue`,`hspQueryFrom`,`hspQueryTo`,`hspHitFrom`,`hspHitTo`,`hspQueryFrame`,`hspHitFrame`,`hspIdentity`,`hspPositive`,`hspGaps`,`hspAlignLen`,`hspQuerySeq`,`hspHitSeq`,`hspMidLine`) VALUES('','$BlastParam_idBlastParam','$queryDef','$queryLen','$queryID','$hitNum','$hitID','$hitDefinition','$hitAccession','$hitLen','$hspNum','$hspBitScore','$hspScore','$hspEvalue','$hspQueryFrom','$hspQueryTo','$hspHitFrom','$hspHitTo','$hspQueryFrame','$hspHitFrame','$hspIdentity','$hspPositive','$hspGaps','$hspAlignLen','$hspQuerySeq','$hspHitSeq','$hspMidLine');\n";
			# 
			# ...
			#
		}
	}
	
	if(!$i++){
		# my $idBlastParam
		my $blastProgram = $result->algorithm();
		my $blastVersion = $result->algorithm_version();
		my $blastReference = $result->algorithm_reference();
		$blastReference =~ s/\'/\\\'/g;
		my $blastDB = $result->database_name();
		my $matrix = $result->get_parameter('matrix');
		my $expect = join ";", $result->available_parameters();
		my $gapOpen = $result->get_parameter('gapopen');
		my $gapExtend = $result->get_parameter('gapext');
		$blastParam = "INSERT INTO `BlastParam`(`idBlastParam`,`blastProgram`,`blastVersion`,`blastReference`,`blastDB`,`matrix`,`expect`,`gapOpen`,`gapExtend`) VALUES('','$blastProgram','$blastVersion','$blastReference','$blastDB','$matrix','$expect','$gapOpen','$gapExtend');\nUPDATE `BlastResult` SET `BlastParam_idBlastParam`=LAST_INSERT_ID() WHERE `BlastParam_idBlastParam`='$BlastParam_idBlastParam';\n";
	}
}

print $blastParam;


